<?php

namespace app\common\model\Depot;

use think\Model;

class Supplier extends Model
{
  // 数据表
  protected $name = 'depot_supplier';

  // 开启自动写入时间戳字段
  protected $autoWriteTimestamp = true;

  // 设置时间戳字段名
  protected $createTime = 'createtime';

  // 设置不更新时间戳
  protected $updateTime = false;

  protected $append = [
    'createtime_text',
    'province_text',
    'city_text',
    'district_text',
  ];

  public function getCreatetimeTextAttr($value, $data)
  {
    $createtime = isset($data['createtime']) ? $data['createtime'] : '';

    if (empty($createtime)) {
      return '';
    }

    return date('Y-m-d H:i:s', $createtime);
  }

  // 省
  protected function getProvinceTextAttr($value, $data)
  {   
      $code = empty($data['province']) ? '' : trim($data['province']);

      return model('Region')->where(['code' => $code])->value('name');
  }

  // 市
  protected function getCityTextAttr($value, $data)
  {
      $code = empty($data['city']) ? '' : trim($data['city']);

      return model('Region')->where(['code' => $code])->value('name');
  }

  // 区
  protected function getDistrictTextAttr($value, $data)
  {
      $code = empty($data['district']) ? '' : trim($data['district']);

      return model('Region')->where(['code' => $code])->value('name');
  }
}
